package Week6.J0525;

// 小根堆

import java.util.PriorityQueue;
import java.util.Scanner;

public class 模板_哈夫曼编码_x {
    public static void main(String[] args) {
        PriorityQueue<Long> heap = new PriorityQueue<>();
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n-- != 0) {
            heap.offer(scanner.nextLong());
        }

        long ret = 0;
        while (heap.size() > 1) {
            long t1 = heap.poll();
            long t2 = heap.poll();
            heap.offer(t1 + t2);
            ret += t1 + t2;
        }

        System.out.println(ret);
    }
}
